﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web.Mvc;
using GWEB.WebBase.Data;
using GWEB.WebBase.Entity;
using GWEB.WebBase.Utility;
using GWEB.WebFrame.Public;
using Newtonsoft.Json;

namespace GWEB.WebFrame.Schedule
{
    public class TeacherClassController : PageInfoBase
    {
        [ActionFilter(IsLogin = true)]
        public ActionResult Index()
        {
            String weekFirst = BasePage.bd.ExecuteScalarString("select convert(varchar(10),getdate()-datepart(weekday,getdate()-1)+1,120)");
            ViewBag.weekFirst = weekFirst;
            return View(BasePage.V("Schedule/TeacherClass"));
        }
        //按天排课
        [ActionFilter(IsJson = true)]
        public JsonResult DayData(String date,int range)
        {
            date = Fun.cSql(date);
            Dictionary<String, Object> data = new Dictionary<String, Object>();
            DataRow drUser = BasePage.GetUser(Session);
            String leagueId = Fun.cString(drUser["LeagueId"]);
            String userId= Fun.cString(drUser["iUserId"]);

            //教室
            String sql = String.Format("select id,name from cu_ClassRoom where state=1 and LeagueId={0} order by id",leagueId);
            Dictionary<String, Object>[] header = BasePage.bd.ExecuteDictArray(sql);
            data.Add("header", header);
            //上课时段
            sql = String.Format("select id,startTime,endTime from cu_Time where state=1 and LeagueId={0} order by startTime,endTime", leagueId);
            Dictionary<String, Object>[] time = BasePage.bd.ExecuteDictArray(sql);
            data.Add("time", time);
            //教师排课
            String cond = "";
            if (range == 1) cond += String.Format(" and TeacherId={0} and TwoTeacherId={0}", userId);
            sql = String.Format(@"select 
    t1.id,'' keyCode, t1.ClassRoomId, t1.CourseName, t1.TimeId, t1.StudentNumber, t1.Color,t1.TeacherName,t1.TwoTeacherName,
	isnull(t2.Count,0) Count,isnull(t2.noSignCount,0) noSignCount,isnull(t2.signCount,0) signCount,isnull(t2.truancyCount,0) truancyCount,isnull(t2.leaveCount,0) leaveCount	
from cu_TeacherCourse t1
left
join (
select Tid, count(*) Count, 
	sum(case when state = 0 then 1 else 0 end) noSignCount,
	sum(case when state = 1 then 1 else 0 end) signCount,
	sum(case when state = 3 then 1 else 0 end) truancyCount,
	sum(case when state = 4 then 1 else 0 end) leaveCount
from cu_TeacherCourse_Sign group by Tid
) t2 on t1.id = t2.Tid
where t1.LeagueId = {0} and TeachDate = '{1}' {2}", leagueId, date,cond);
            Dictionary<String, Object>[] teacherCourse = BasePage.bd.ExecuteDictArray(sql);
            foreach (Dictionary<String, Object> dr in teacherCourse)
                dr["keyCode"] = Fun.IDCode(Fun.cString(dr["id"]));
            data.Add("teacherCourse", teacherCourse);
            //签到情况
            sql = String.Format(@"select tid,t2.state,CustName  
from
cu_TeacherCourse t1
inner join cu_TeacherCourse_Sign t2 on t1.id = t2.tid
inner join cu_customers t3 on t2.cid = t3.id
where t1.LeagueId = {0} and TeachDate = '{1}' {2}
order by tid, t2.state", leagueId, date, cond);
            Dictionary<String, Object>[] sign = BasePage.bd.ExecuteDictArray(sql);
            data.Add("sign", sign);

            return BasePage.Json(ResultData.OK(data));
        }
        //按周排课
        [ActionFilter(IsJson = true)]
        public JsonResult WeekData(String date, int range)
        {
            Dictionary<String, Object> data = new Dictionary<String, Object>();
            DataRow drUser = BasePage.GetUser(Session);
            String leagueId = Fun.cString(drUser["LeagueId"]);
            String userId = Fun.cString(drUser["iUserId"]);
            //星期
            String sql = String.Format("select did as id,dname as name,convert(varchar(5),dateadd(d,did-1,'{0}'),110) date from tb_sys_dictionary where typeid='C15'", date);
            Dictionary<String, Object>[] header = BasePage.bd.ExecuteDictArray(sql);
            data.Add("header", header);            
            //上课时段
            sql = String.Format("select id,startTime,endTime from cu_Time where state=1 and LeagueId={0} order by startTime,endTime", leagueId);
            Dictionary<String, Object>[] time = BasePage.bd.ExecuteDictArray(sql);
            data.Add("time", time);
            //教师排课
            String cond = "";
            if (range == 1) cond += String.Format(" and TeacherId={0} and TwoTeacherId={0}", userId);
            sql = String.Format(@"select 
    t1.id,'' keyCode, datepart(weekday,TeachDate-1) as weekId, t1.CourseName, t1.TimeId, t1.StudentNumber, t1.Color,t1.TeacherName,t1.TwoTeacherName,convert(varchar(10),TeachDate,120) TeachDate
from cu_TeacherCourse t1
where t1.LeagueId = {0} and TeachDate>='{1}' and TeachDate-7<'{1}' {2} ", leagueId, date, cond);
            Dictionary<String, Object>[] teacherCourse = BasePage.bd.ExecuteDictArray(sql);
            foreach (Dictionary<String, Object> dr in teacherCourse)
                dr["keyCode"] = Fun.IDCode(Fun.cString(dr["id"]));
            data.Add("teacherCourse", teacherCourse);
            return BasePage.Json(ResultData.OK(data));
        }
        //按月排课
        [ActionFilter(IsJson = true)]
        public JsonResult MonthData(String date, int range)
        {
            Dictionary<String, Object> data = new Dictionary<String, Object>();
            DataRow drUser = BasePage.GetUser(Session);
            String leagueId = Fun.cString(drUser["LeagueId"]);
            String userId = Fun.cString(drUser["iUserId"]);
            //星期
            String sql = String.Format("select did as id,dname as name from tb_sys_dictionary where typeid='C15'");
            Dictionary<String, Object>[] header = BasePage.bd.ExecuteDictArray(sql);
            data.Add("header", header);
            //教师排课
            String cond = "";
            if (range == 1) cond += String.Format(" and TeacherId={0} and TwoTeacherId={0}", userId);
            sql = String.Format(@"select 
    t1.id,t1.CourseName, t1.TimeId, t1.StudentNumber, t1.Color,t1.TeacherName,t1.TwoTeacherName,day(TeachDate) day
from cu_TeacherCourse t1
where t1.LeagueId = {0} and TeachDate>='{1}' and dateadd(MM,-1,TeachDate)<'{1}' {2} ", leagueId, date, cond);
            Dictionary<String, Object>[] teacherCourse = BasePage.bd.ExecuteDictArray(sql);
            data.Add("teacherCourse", teacherCourse);

            sql = String.Format("select datepart(weekday,dateadd(d,-1,'{0}')) startWeekId,datediff(d,'{0}',dateadd(MM,1,'{0}')) dayCount", date);
            DataRow dr = BasePage.bd.ExecuteRow(sql);
            data.Add("startWeekId", dr["startWeekId"]);
            data.Add("dayCount", dr["dayCount"]);
            return BasePage.Json(ResultData.OK(data));
        }
    }
}